﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;
using EncryptionLib;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;
using EncryptionLib.Users;
using System.Text;
using System.Collections.Generic;

namespace TotalCommander_v3
{
    public partial class frmManageUser : Form
    {
        public frmManageUser()
        {
            InitializeComponent();
        }

        #region private utilities

        private void BrowseXMLFile(TextBox txt, bool bOpen)
        {
            FileDialog dlg;
            if (bOpen)
            {
                dlg = new OpenFileDialog();
                dlg.AutoUpgradeEnabled = dlg.CheckFileExists = dlg.CheckPathExists = true;
                dlg.Title = "Open a xml file.";
            }
            else
            {
                dlg = new SaveFileDialog();
                dlg.Title = "Save to a xml file.";
            }
            dlg.Filter = "XML files (*.xml)|*.xml||";

            if (dlg.ShowDialog(this) == DialogResult.OK)
                txt.Text = dlg.FileName;
        }

        private void UpdateEncryptZip()
        {


        }

        private void frmManageUser_Load(object sender, EventArgs e)
        {
            GetData();
        }

        private void GetData()
        {
            List<UserData> lUser = new List<UserData>();
            for (int i = 0; i < frmMain.m_userManager.GetCount(); i++)
            {
                lUser.Add(frmMain.m_userManager.GetUser(i));
            }

            if (lUser.Count > 0)
                this.cbUserToExport.DataSource = lUser;
            else
            {
                MessageBox.Show("You don't have any users!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.btnUserExport.Enabled = false;
            }
            
        }
        private void llblUserExport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            BrowseXMLFile(tbExportFile, false);
        }

        private void btnUserExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(tbExportFile.Text))
                {
                    UserData userData = frmMain.m_userManager.GetUser(cbUserToExport.SelectedIndex);
                    Debug.Assert(UserManagerXML.ExportUserData(userData, tbExportFile.Text));
                    MessageBox.Show("Export successful!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Export file can not be null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception)
            {
                MessageBox.Show("Export Failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void llblUserImport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            BrowseXMLFile(tbImportFile, true);
        }

        private void btnUserImport_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(tbImportFile.Text))
                {
                    UserData user = UserManagerXML.ImportUserData(tbImportFile.Text);
                    Debug.Assert(user != null);
                    bool result = frmMain.m_userManager.Register(user);
                    if (!result)
                    {
                        MessageBox.Show("Import Failed!\n New email must be different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    // save data 
                    frmMain.m_userManager.SaveData();
                    // update combobox
                    GetData();
                    MessageBox.Show("Import successful!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Import file can not be null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception)
            {
                MessageBox.Show("Import Failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        #endregion
    }
}
